<%@ page import="main.java.utils.StudentManager" %>
<%@ page import="main.java.utils.Student" %>
<%@ page import="java.util.List" %>
<%--
  Created by IntelliJ IDEA.
  User: My
  Date: 2020/10/15
  Time: 14:50
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>

<%
    // 自动登录测试
    Cookie[] cookies = request.getCookies();
    String user = "";
    String password = "";
    if(cookies != null && cookies.length > 0){
        for(Cookie c: cookies) {
            if ("user".equals(c.getName())) {
                user = c.getValue();  // 得到cookie的用户名
                user = java.net.URLDecoder.decode(user,"UTF-8");// 解码
            }
            if ("password".equals(c.getName())) {
                password = c.getValue(); // 得到cookie的密码
            }
        }
    }
    if(!user.equals("") && !password.equals("")){// 不是首次登录
        StudentManager studentManager = new StudentManager();
        List<Student> list = studentManager.findAll();
        request.setAttribute("stu", list);
        request.getRequestDispatcher("/stuList.jsp").forward(request, response); // 跳转到学生列表
    }else{

    }
%>
<html>
<head>
    <title>用户登录</title>
    <style>
        /* 让页面所有元素的padding和margin都设置为0 */
        *{margin:0;padding:0;box-sizing:border-box;}
        /* 设置背景图，字体设置为微软雅黑 */
        body{font-family: "微软雅黑", sans-serif;}
        /* 引用图片高度设置为28px，就是页面最上方像屋檐一样的黑色图 */
        .headtop{height:28px;}
        /* 整个登录框的css，并使用绝对定位居中 */
        .login {
            position: absolute;
            top: 50%;
            left: 50%;
            margin: -150px 0 0 -150px;
            width:300px;
            height:300px;
        }
        /* 前面分析过的h1标签的css，text-shadow设置阴影使文字更好看，letter-spacing设置字符间距 */
        .login h1 { color:#555555; text-shadow: 0px 10px 8px #CDC673; letter-spacing:2px;text-align:center;margin-bottom:20px; }
        /* 两个输入框的css，border属性设置边框线粗细以及颜色，border-radius设置边框的圆角角度 */
        input{
            padding:10px;
            width:100%;
            color:white;
            margin-bottom:10px;
            background-color:#555555;
            border: 1px solid black;
            border-radius:4px;
            letter-spacing:2px;
        }
        /* 登录按钮的css，cursor:pointer当鼠标移到按钮上面时变成小手形状 */
        form button{
            width:100%;
            padding:10px;
            background-color:#CDC673;
            border:1px solid black;
            border-radius:4px;
            cursor:pointer;
        }
    </style>
</head>

<body>
    <div class="headtop"></div>
        <div class="login">
        <h1>用户登录</h1>
        <form action="login.do" method="post">
            <input id="name" type="text" name="user" placeholder="用户名" required="required" onblur="checkName()"><span id="userErr"></span>
            <input id = "pass" type="password" name="password" placeholder="密  码" required="required" onblur="checkPass()"><span id="passErr"></span>
            <input type="checkbox" name="check" id="mm" style="width:50px;">记住密码，自动登录
            <button type="submit" onclick="return checkForm(this.form)">登录</button>
        </form>
    </div>
</body>
</html>
<%--<script src="./js/jquery-2.1.4.js"></script>--%>
<script>
    function checkName() {
        //alert($("#name").val());
        var name = document.getElementById("name");
        var userErr=document.getElementById("userErr");
        if( ""== name.value.replace(/\s/g,"")){
            userErr.innerHTML = "用户名不能为空";
            userErr.style.color = "red";
            return false;
        }else{
            name.value=name.value.replace(/\s/g,"");//除去空格
            userErr.innerHTML="";
            return true;
        }
    }

    function checkEmail() {
        var email=document.getElementById("email").value;
        var emailErr=document.getElementById("emailErr");
        var reg = /^[1-9]\d{7,10}@qq+\.com/; // js正则表达式必须用写在内,不然无法识别test方法//
        if(!reg.test(email)) {
            emailErr.innerHTML = "邮箱格式不正确";
            emailErr.style.color = "red";
            return false;
        }else {
            emailErr.innerHTML = "";
            return false;
        }
    }

    function checkPass() {
        var pass = document.getElementById("pass");
        var passErr = document.getElementById("passErr");
        if( ""== pass.value.replace(/\s/g,"")){
            passErr.innerHTML = "密码不能为空";
            passErr.style.color = "red";
            return false;
        }else{
            pass.value=pass.value.replace(/\s/g,"");//除去空格
            passErr.innerHTML="";
            return true;
        }
    }

    function checkForm(){
        if(checkName() === true && checkPass() === true){
            return true
        }
    }

</script>
